#!/bin/sh

# only work on an Ethernet interface
if [ "eth0" != "$IFACE" ]; then
	exit 0
fi

. /usr/share/common

nfsip=$(sed -n '/^[^ ]*:.* \/ nfs.*[ ,]addr=\([0-9.]\+\).*/s//\1/p' /proc/mounts)

# Check if the interface is not a USB device, then set the MAC address, unless using nfsroot
if [ -n "$nfsip" ] && ip route get "$nfsip" | grep -q "dev $IFACE" >/dev/null; then
	echo_error "Skip MAC address setup for $IFACE, used for NFS from $nfsip"
	exit 0
elif [ -d /sys/class/net/eth0 ] && ! readlink -f /sys/class/net/eth0/device | grep -q "usb" >/dev/null; then
	if [ -n "$ethaddr" ]; then
		echo_info "Set MAC address $ethaddr (from environment) on $IFACE"
		ip link set dev "$IFACE" address "$ethaddr"
	fi
else
	echo_error "$IFACE does not exist or is a USB device, skipping MAC address setup"
	exit 0
fi

exit 0
